﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using static CoreShop.Library.GlobalEnumVars;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsuserbalanceServices : Repository<corecmsuserbalance>, IcorecmsuserbalanceServices
    {
        public bool InsertAsync(corecmsuserbalance entity)
        {
            return base.Change<corecmsuserbalance>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsuserbalance> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsuserbalance entity)
        {
            return base.Change<corecmsuserbalance>().Update(a => new corecmsuserbalance()
            {
                orderId = entity.orderId,
                userId = entity.userId,
                type = entity.type,
                storeId = entity.storeId,
                paymentId = entity.paymentId,
                money = entity.money,
                giftAmount = entity.giftAmount,
                balance = entity.balance,
                giftBalance = entity.giftBalance,
                sourceId = entity.sourceId,
                memo = entity.memo,
                createTime = entity.createTime,
                corpId = entity.corpId,
                cardItemId = entity.cardItemId,
                quantity = entity.quantity,
                balanceQuantity = entity.balanceQuantity,
                sysUserId = entity.sysUserId,
                payStatus = entity.payStatus,
                paymentCode = entity.paymentCode,
                status = entity.status
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsuserbalance> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsuserbalance>().Update(a => new corecmsuserbalance()
                {
                    orderId = item.orderId,
                    userId = item.userId,
                    type = item.type,
                    storeId = item.storeId,
                    money = item.money,
                    giftAmount = item.giftAmount,
                    balance = item.balance,
                    giftBalance = item.giftBalance,
                    sourceId = item.sourceId,
                    memo = item.memo,
                    createTime = item.createTime,
                    corpId = item.corpId,
                    cardItemId = item.cardItemId,
                    quantity = item.quantity,
                    balanceQuantity = item.balanceQuantity,
                    paymentCode = item.paymentCode,
                    sysUserId = item.sysUserId
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            //return base.DeleteById(id);
            return base.Change<corecmsuserbalance>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsuserbalance>().DeleteById(id);
            }
            return returnKey;
        }


        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsuserbalance> GetCaChe()
        {
            return base.GetList();
        }
        #endregion
        public List<corecmsuserbalance> SelectByCorpId(int corpId, int storeId)
        {
            return base.GetList(a => a.corpId == corpId && a.storeId == storeId).OrderBy(a => a.id).ToList();
        }

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsuserbalance> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsuserbalance> custos = new List<corecmsuserbalance>();
            var where = Expressionable.Create<corecmsuserbalance>();
            //where = where.And(a => a.isDelete == false);
            //if (!string.IsNullOrEmpty(sName))
            //{
            //    where = where.And(a => a.name.Contains(sName))
            //        .Or(a => a.number.Contains(sName)).Or(a => a.name.Contains(sName));
            //}
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.id).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsuserbalance> QueryAsync(long typeId)
        {
            List<corecmsuserbalance> list = new List<corecmsuserbalance>();
            var where = Expressionable.Create<corecmsuserbalance>();
            //where = where.And(a => a.isDelete == false && a.id > 0);
            if (typeId > 0)
            {
                where = where.And(a => a.id == typeId);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
        #endregion

    }
}
